package com.example.administrator.szb.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.activity.layout.RZActivity;
import com.example.administrator.szb.activity.layout.RZCenterActivity2;
import com.example.administrator.szb.bean.UserBase;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.eventbus.Event;
import com.example.administrator.szb.eventbus.eventbean.RegisterInfoBeanEvent;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.FinishActivity;
import com.example.administrator.szb.util.IosDiaolog;
import com.example.administrator.szb.util.JPushUtils;
import com.example.administrator.szb.util.SPUtils;
import com.example.administrator.szb.util.ScreenExpressionUtils;
import com.example.administrator.szb.util.SpannableStringUtil;
import com.example.administrator.szb.util.TokenUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by 了不起的潘 on 2017/12/20.
 */

public class RegisterActivity_new extends BaseActivity {
    @Bind(R.id.register_choose_button_register)
    Button registerChooseButtonRegister;
    @Bind(R.id.register_choose_text_xy)
    TextView registerChooseTextXy;
    private String phone_num = "";//手机号
    private String password = "";//密码
    int type = -1;//-1,没有选择，0，普通用户，1，金融顾问
    String str_tips="";
    private CardView cardView_GuWen,cardView_User;
    private ImageView iv_guwen,iv_user,iv_guwen_check,iv_user_check;
    private TextView tv_guwen_check,tv_user_check,tips;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_new_activity);
        ButterKnife.bind(this);
        initBindViews();
        SampleApplicationLike.getActivitiesInstance().add(this);
        initToolbar(R.id.register_new_toolbar_include, "身份选择");
        initListener();
    }

    private void initBindViews() {
        tips = (TextView) findViewById(R.id.tips);
        cardView_GuWen = (CardView) findViewById(R.id.cardView_GuWen);
        iv_guwen = (ImageView) findViewById(R.id.iv_guwen);
        iv_guwen_check = (ImageView) findViewById(R.id.iv_guwen_check);
        tv_guwen_check = (TextView) findViewById(R.id.tv_guwen_check);
        cardView_User = (CardView) findViewById(R.id.cardView_User);
        iv_user = (ImageView) findViewById(R.id.iv_user);
        iv_user_check = (ImageView) findViewById(R.id.iv_user_check);
        tv_user_check = (TextView) findViewById(R.id.tv_user_check);
    }

    private void initListener() {
        cardView_GuWen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reset();
                type=1;
                iv_guwen_check.setImageResource(R.mipmap.check_yes_icon);
                iv_guwen.setImageResource(R.mipmap.guwen_yes);
                tv_guwen_check.setTextColor(getResources().getColor(R.color.basecolor));
                tips.setText("您选择的身份是 金融顾问");
                str_tips = "金融顾问";
            }
        });
        cardView_User.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type = 0;
                reset();
                iv_user_check.setImageResource(R.mipmap.check_yes_icon);
                iv_user.setImageResource(R.mipmap.user_yes);
                tv_user_check.setTextColor(getResources().getColor(R.color.basecolor));
                tips.setText("您选择的身份是 上市公司");
                str_tips = "上市公司";

            }
        });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveEvent(Event event) {
        if (event.getCode() == R.layout.register_info_activity && event.getData() != null) {
            RegisterInfoBeanEvent bean = (RegisterInfoBeanEvent) event.getData();
            phone_num = bean.getPhone_num();
            password = bean.getPassword();
            //Toasts.show(this, "phone_num:"+ bean.getPhone_num(), Toast.LENGTH_SHORT);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @OnClick({R.id.register_choose_button_register, R.id.register_choose_text_xy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.register_choose_button_register:
                requsetRegister();
                break;
            case R.id.register_choose_text_xy:
                Intent intent = new Intent(RegisterActivity_new.this,GZXYActivity.class);//额，
                intent.putExtra("title","市值宝服务协议");
                intent.putExtra("id",4);
                startActivity(intent);
                break;
        }
    }

    private void reset(){
        iv_guwen_check.setImageResource(R.mipmap.check_no_icon);
        iv_user_check.setImageResource(R.mipmap.check_no_icon);
        iv_guwen.setImageResource(R.mipmap.guwen_no);
        iv_user.setImageResource(R.mipmap.user_no);
        tv_guwen_check.setTextColor(getResources().getColor(R.color.black1));
        tv_user_check.setTextColor(getResources().getColor(R.color.black1));
    }

    private void requsetRegister() {
        boolean flag = checkInfo();
        if (flag){

            iosDiaolog.reset();
            SpannableStringUtil s = new SpannableStringUtil(iosDiaolog.getTips());
            s.addText("请确认您选择的身份是 ");
            s.addTextWithColor(str_tips,getResources().getColor(R.color.basecolor));
            iosDiaolog.setMsgs2(s.getText());
            iosDiaolog.setRightClick(new IosDiaolog.OnIosDialogClickListener() {
                @Override
                public void onClick(String str, PopupWindow popupWindows) {
                    doRequsetRegister();
                }
            });
            iosDiaolog.alert(tv_user_check);

        }
    }



    private boolean checkInfo() {
        if (TextUtils.isEmpty(phone_num)){

        }else if(TextUtils.isEmpty(password)){

        }else if (type == -1){
            DialogUtil.showToast(this,"请选择身份呀");
        }
        else{
            return true;
        }
        return false;
    }

    /**
     * 执行注册请求
     */
    private void doRequsetRegister() {
        registerChooseButtonRegister.setEnabled(false);
        DialogUtil.showIsoProgressbar(RegisterActivity_new.this,"注册中...");
        Map<String,Object> map = new HashMap<>();
        String code = TokenUtil.md5("szb").substring(0, 8);
        String pass = password;
        final String tel = phone_num;
        map.put("code", HttpUtil.getParam());
        map.put("genre", type + 1 + "");//
        map.put("pass", pass);
        map.put("tel", tel);
        String url = URLAddress.URLBASE + URLAddress.URL_ZC;
        //String json = GetJsonDataUtil.simpleMapToJsonStr(map);
        HttpUtils.post(activity,url, map, new OnResultListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onSuccess(int requestCode, String str) {
                final UserBase userBase = JSON.parseObject(str,new TypeReference<UserBase>(){});
                SPUtils.setDatas(SPUtils.USER_ID,userBase.getId());
                SPUtils.setDatas(SPUtils.USER_PHONE,phone_num);
                JPushUtils.register(phone_num,userBase.getJpushpass());
                JPushUtils.setALias(RegisterActivity_new.this,phone_num);
                SPUtils.setDatas(SPUtils.USER_BASE,str);
                DialogUtil.dialog_ios.dismiss();
                DialogUtil.showPopupwindow(RegisterActivity_new.this, R.layout.view_popupwindow_register_success,
                        ScreenExpressionUtils.dip2px(RegisterActivity_new.this,295),
                        ScreenExpressionUtils.dip2px(RegisterActivity_new.this,380),
                        R.layout.register_new_activity, new CallbackPopuwindow() {
                            @Override
                            public void doSomeThing(View view) {
                                view.findViewById(R.id.register_success_xkk).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        DialogUtil.popWindow.dissmiss();
                                        FinishActivity.finish();
                                        Intent intent = new Intent(RegisterActivity_new.this,HomeActivity.class);
                                        startActivity(intent);
                                        finish();
                                    }
                                });

                                view.findViewById(R.id.register_success_ljrz).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        DialogUtil.popWindow.dissmiss();
                                        Intent intent;
                                        FinishActivity.finish();
                                        HomeActivity.IS_LOAD = false;
                                        if (userBase.getType()==1){
                                            intent = new Intent(RegisterActivity_new.this,RZActivity.class);
                                        }else {
                                            intent = new Intent(RegisterActivity_new.this,RZCenterActivity2.class);
                                        }

                                        startActivity(intent);
                                        finish();
                                    }
                                });
                            }
                        });
            }

            @Override
            public void onFailure(int requestCode, String str) {
                registerChooseButtonRegister.setEnabled(true);
                DialogUtil.dialog_ios.dismiss();
                DialogUtil.showToast(RegisterActivity_new.this,str);
            }
        });
    }
}
